package com.brsya.movie.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.brsya.base.base.Contents;
import com.brsya.base.bean.MovieBean;
import com.brsya.base.bean.MovieListBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.base.recycler.BaseItemBean;
import com.brsya.base.recycler.BaseViewHolder;
import com.brsya.base.util.LoggerUtil;
import com.brsya.movie.R;
import com.brsya.movie.activity.PlayActivity;
import com.brsya.movie.util.AppUtil;
import com.brsya.movie.widget.MovieTable;
import com.bytedance.sdk.dp.DPSdk;
import com.bytedance.sdk.dp.DPWidgetVideoCardParams;
import com.bytedance.sdk.dp.IDPAdListener;
import com.bytedance.sdk.dp.IDPElement;
import com.bytedance.sdk.dp.IDPVideoCardListener;
import com.bytedance.sdk.dp.IDPWidgetFactory;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by Brsya
 * CreateDate: 2022/7/6  9:14
 * Description:
 * 影视列表Adapter
 */
public class MovieListAdapter extends BaseAdapter<BaseItemBean> {
    public static final int LAYOUT_TYPE_LIST = 0;
    public static final int LAYOUT_TYPE_AD = 1;
    public static final int LAYOUT_TYPE_AD_VIDEO = 2;

    private final Activity context;

    private OnRefreshListener onRefreshListener;

    private List<View> imgAdViews;

    private View videoAdBox;

    public MovieListAdapter(Activity context) {
        this.context = context;
        imgAdViews = new LinkedList<>();
        loadVideoAd();
    }

    @Override
    public BaseViewHolder onCreate(ViewGroup parent, int viewType) {
        View rootView;
        switch (viewType) {
            case LAYOUT_TYPE_AD:
            case LAYOUT_TYPE_AD_VIDEO:
                rootView = LayoutInflater.from(context).inflate(R.layout.item_movie_list_ad_box, parent, false);
                break;
            case LAYOUT_TYPE_LIST:
            default:
                rootView = LayoutInflater.from(context).inflate(R.layout.item_movie_list, parent, false);
                break;
        }
        return new BaseViewHolder(rootView);
    }

    @Override
    public void onBind(BaseViewHolder viewHolder, int RealPosition, BaseItemBean data) {
        switch (data.getItemType()) {
            case LAYOUT_TYPE_AD:
                // 图文广告
                ViewGroup adBox = viewHolder.findViewById(R.id.fl_ad_box);
                if (adBox.getChildCount() == 0) {
                    loadImgAd(adBox);
                }else{
                    LoggerUtil.e("已有图片广告，无需加载: " + RealPosition);
                }
                break;
            case LAYOUT_TYPE_AD_VIDEO:
                // 精彩小视频
                if (videoAdBox != null) {
                    ViewGroup adVideoBox = viewHolder.findViewById(R.id.fl_ad_box);
                    if (adVideoBox != null && adVideoBox.indexOfChild(videoAdBox) < 0) {
                        adVideoBox.addView(videoAdBox);
                    }
                }
                break;
            case LAYOUT_TYPE_LIST:
            default:
                // 数据
                if (data instanceof MovieListBean.ContentListDTO) {
                    showListData(viewHolder, RealPosition, (MovieListBean.ContentListDTO) data);
                }
                break;

        }
    }

    @Override
    public int getItemViewType(int position) {
        int itemViewType = super.getItemViewType(position);
        if (itemViewType != TYPE_NORMAL) {
            return itemViewType;
        }
        if (getHeaderView() != null) {
            return getDataList().get(position - 1).getItemType();
        }
        return getDataList().get(position).getItemType();
    }

    private void showListData(BaseViewHolder viewHolder, int RealPosition, MovieListBean.ContentListDTO bean) {

        // 标题
        TextView title = viewHolder.findViewById(R.id.tv_list_title);
        title.setText(bean.getTitle());
        // 影视数据
        MovieTable table = viewHolder.findViewById(R.id.mt_movie_box);
        table.setVideoList(bean.getVideoList());
        table.setOnItemClickListener(new MovieTable.OnItemClickListener() {
            @Override
            public void onItemClick(MovieListBean.ContentListDTO.VideoListDTO videoBean) {
                // 进入新页面
                Intent intent = new Intent(context, PlayActivity.class);
                MovieBean movieBean = new MovieBean();
                movieBean.setId(videoBean.getVideoId());
                movieBean.setName(videoBean.getName());
                movieBean.setCoverUrl(videoBean.getCoverUrl());
                movieBean.setVideoTag(bean.getTitle());
                intent.putExtra(Contents.INTENT_MOVIE_DETAIL, movieBean);
                context.startActivity(intent);
            }
        });

        // 看更多
        LinearLayout more = viewHolder.findViewById(R.id.ll_home_move_list_more);
        // 换一换
        LinearLayout refresh = viewHolder.findViewById(R.id.ll_home_move_list_refresh);

        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 进入片单详情页面
                String imgUrl;
                if (bean.getVideoList() != null && bean.getVideoList().size() > 0) {
                    imgUrl = bean.getVideoList().get(0).getCoverUrl();
                } else {
                    imgUrl = null;
                }
                AppUtil.openInventoryDetail(context, bean.getHvId(), bean.getTitle(), imgUrl);
            }
        });

        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onRefreshListener == null) {
                    return;
                }
                onRefreshListener.onRefresh(RealPosition, bean);
            }
        });
    }

    private void loadVideoAd() {

        if (DPSdk.factory() == null) {
            return;
        }

        // 加载穿山甲小视频卡片
        DPSdk.factory().loadCustomVideoCard(
                DPWidgetVideoCardParams.obtain()
                        .cardHeight(134)
                        .listener(new IDPVideoCardListener() {
                            @Override
                            public void onDPItemClick(Map<String, Object> map) {
                                super.onDPItemClick(map);
                                //                                if (getActivity() != null) {
                                //                                    getActivity().getWindow().setNavigationBarColor(0xFF000000);
                                //                                }
                            }

                            @Override
                            public void onDPLSwipeEnter() {
                                super.onDPLSwipeEnter();
                                //                                if (getActivity() != null) {
                                //                                    getActivity().getWindow().setNavigationBarColor(0xFF000000);
                                //                                }
                            }
                        })
                        .adListener(new IDPAdListener() {

                        })
                        .dislikeListener(context, s -> LoggerUtil.e("小视频onSelected: " + s))
                , new IDPWidgetFactory.Callback() {

                    @Override
                    public void onError(int i, String s) {
                        LoggerUtil.e("小视频加载失败: " + i + " --- " + s);
                    }

                    @Override
                    public void onSuccess(IDPElement idpElement) {
                        videoAdBox = idpElement.getView();
                    }
                });
    }

    @Override
    public void setDataList(List<BaseItemBean> dataList) {
        super.setDataList(dataList);
    }

    /**
     * 加载图片广告
     */
    private void loadImgAd(ViewGroup adBox) {
        LoggerUtil.e("加载图片广告");
        // 加载广告
        TTAdNative adNative = TTAdSdk.getAdManager().createAdNative(context);

        AdSlot adSlot = new AdSlot.Builder()
                // 广告ID 948498437 948498604
                .setCodeId(Contents.AD_TT_SDK_MOVIE_ID)
                .setSupportDeepLink(true)
                .setAdCount(1)
                // 期望模板广告view的size,单位dp
                .setExpressViewAcceptedSize(400, 0)
                .setAdloadSeq(1)
                .build();
        adNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int i, String s) {
                LoggerUtil.e("穿山甲广告加载失败" + i + "--ss--" + s);
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                LoggerUtil.e("广告加载成功");
                for (TTNativeExpressAd adBean : list) {
                    View adView = adBean.getExpressAdView();
                    adBox.removeAllViews();
                    adBox.addView(adView);
                    LoggerUtil.e("广告添加成功");
                    adBean.render();
                    adBean.setDislikeCallback(context, new TTAdDislike.DislikeInteractionCallback() {
                        @Override
                        public void onShow() {
                            adBox.removeAllViews();
                        }

                        @Override
                        public void onSelected(int i, String s, boolean b) {

                        }

                        @Override
                        public void onCancel() {

                        }
                    });
                    break;
                }
            }
        });
    }


    public OnRefreshListener getOnRefreshListener() {
        return onRefreshListener;
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    /**
     * 换一换监听
     */
    public interface OnRefreshListener {
        /**
         * 换一换
         *
         * @param position 所在position
         * @param bean     数据
         */
        void onRefresh(int position, MovieListBean.ContentListDTO bean);
    }
}
